package com.ruoyi.common.utils;

import com.ruoyi.common.core.redis.RedisCache;
import com.ruoyi.common.utils.spring.SpringUtils;

import java.util.Date;
import java.util.Map;

public class redisCacheUtils {

    public static String updatetime = "updatetime";

    public static boolean getupdatetime(Long currenttime, Boolean isquery) {
        boolean flag = false;
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        String key = "tree:" + updatetime + SecurityUtils.getCompanyId();
        String time = redisCache.getCacheObject(key);
        if (StringUtils.isEmpty(time)) {
            //更新时间为空
            setupdatetime();
            flag = true;
        } else {
            //数据时间小于缓存时间
            if(currenttime!=null) {
                if (currenttime.longValue() < Long.valueOf(time).longValue()) {
                    flag = true;
                }
            }
            //参数强制更新
            if (isquery == true) {
                flag = true;
            }
        }
        return flag;
    }

    public static boolean getupdatetime(Map<String, Object> params) {
        boolean flag = false;
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        String key = "tree:" + updatetime + SecurityUtils.getCompanyId();
        Object time = redisCache.getCacheObject(key);
        Object currenttime=params.get("currenttime");
        Object isquery=params.get("isquery");
        if (time==null||(time!=null&&StringUtils.isEmpty(time.toString()))) {
            //更新时间为空
            setupdatetime();
            flag = true;
        } else {
            //数据时间小于缓存时间
            if (currenttime != null && StringUtils.isNotEmpty(currenttime.toString())) {
                if (Long.valueOf(currenttime.toString()).longValue() < Long.valueOf(time.toString()).longValue()) {
                    flag = true;
                }
            }
            //参数强制更新
            if (isquery != null && StringUtils.isNotEmpty(isquery.toString())) {
                if (isquery.toString().equals("true")) {
                    flag = true;
                }
            }
            //没有关键参数默认更新
            if(currenttime==null&&isquery==null){
                flag=true;
            }
        }
        return flag;
    }

    public static void setupdatetime() {
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        String key = "tree:" + updatetime + SecurityUtils.getCompanyId();
        Date currentdate = new Date();
        redisCache.setCacheObject(key, currentdate.getTime());
    }

    public static Long gettime(){
        RedisCache redisCache = SpringUtils.getBean(RedisCache.class);
        String key = "tree:" + updatetime + SecurityUtils.getCompanyId();
        return redisCache.getCacheObject(key);
    }
}
